/**
 * Copyright © 2011 looty - Textoo
 * @link http://www.textoo.org
 * @link http://code.google.com/p/textoo/
 * @author lab9 - Bertrand Larrieu
 * @mail lab9.fr@gmail.com
 * @version 2.0
 */

package org.looty.core.textoo.render 
{
	import flash.events.*;
	import flash.text.*;
	import org.looty.core.textoo.format.*;
	import org.looty.textoo.render.*;

	public class AbstractTextooRenderer extends EventDispatcher
	{
		
		private var _id					:uint;
		static private const JUSTIFIER	:String = " …                               ";
		
		/** 
		* Embed Font
		*/
		[Embed(source="textoo.ttf", mimeType="application/x-font-truetype", fontName="textoo", fontStyle="normal", fontWeight="normal", unicodeRange="U+00A0,U+2026", embedAsCFF="false")]
		static public const TEXTOO_FONT:Class;
		
		static private const FONT:Font = new TEXTOO_FONT();		
		
		public function AbstractTextooRenderer(id:uint) 
		{
			_id = id;
		}
		
		protected function createTextField():TextField
		{
			var textField:TextField = new TextField();
			textField.multiline = true;
			textField.selectable = false;
			textField.antiAliasType = AntiAliasType.ADVANCED;
			textField.gridFitType = GridFitType.PIXEL;
			
			return textField;
		}
		
		protected function applyProperties(textField:TextField, props:TextooProps):void
		{
			
			textField.width = props.size.width -2;
			textField.height = props.size.height;
			
			textField.wordWrap = props.wordWrap;
			textField.embedFonts = props.embedFonts;
			textField.sharpness = props.sharpness;
			textField.thickness = props.thickness;
			textField.insertXMLText(0, 0, props.richText);
			var textFormat:TextFormat = textField.getTextFormat(props.length - 1);			
			
			if (props.isOverflowing && textFormat.align == TextFormatAlign.JUSTIFY  && !(/\s/.test(props.text.charAt(props.length - 1)))) 
			{
				textField.appendText(JUSTIFIER);
				if (props.embedFonts)
				{
					textFormat.font = FONT.fontName;
					textFormat.bold = false;
					textFormat.italic = false;
					textField.setTextFormat(textFormat, props.length + 1, textField.length);
				}
				
			}
		}		
		
		public function dispose():void
		{
			
		}
		
		public function get id():uint 
		{
			return _id;
		}
		
	}

}